package com.lagou.edu.utils;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 进行事务管理，主要封装了方法执行前后的事务开启、提交和回滚
 */
@Service
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    //通过set方法进行注入连接
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    //开启事务
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
        System.out.println("开启事务");
    }
    //提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
        System.out.println("提交事务");
    }
    //回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
        System.out.println("回滚事务");
    }
}